<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
  <title>Document</title>
  <style>

  </style>
</head>

<body>

</body>
<script>
  //封装一个函数,作用和  axios一样
  function ajax({ method, url, params, data }) {
    let arr = []
    for (let key in params) {
      arr.push(`${key} = ${params[key]}`)
    }

    const xhr = new XMLHttpRequest()
    xhr.addEventListener('load', function () {
      if (xhr.status >= 200 && xhr.status < 300) {
        console.log('成功', JOSN.parse(xhr.response));
      } else if (xhr.status >= 300) {
        console.log('失败', JOSN.parse(xhr.response));
      }
    })

    xhr.open(method, url + '?' + arr.join('&'))

    if (typeof data === 'string') {
      //说明提交的是查询字符串
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
      xhr.send(data)
    } else if (data instanceof FormData) {
      //提交的是formdata 对象
      xhr.send(data)
    } else if (data instanceof Object) {
      xhr.setRequestHeader('Content-Type', 'application/json')
      xhr.send(JSON.stringify(data))
    } else {
      xhr.send()
    }
  }
</script>

</html>